using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

    /// <summary>
    /// This Class is primarily used to create a new XSD for a Biztalk project.  The thought would be that a console appplication
    /// would use this class to create the document for use within the Solution.  Basically this allows for generation of a 
    /// XSD from an activity definition made inside the BAM.XLA add in within Excel.  As items are added to the Activity this utility would be
    /// rerun to generate an updated XSD that would contain the new activity data definitions.
    /// </summary>
    public class BAM_XSD_Builder
    {
        public static string BuildBAMXSD(XmlDocument BAMExportedDocument, string NameSpace)
        {
            //Used to build a new XSD document for BAM tracking
            //Parse the existing document 
            //Root - <BAMDefinition xmlns="http://schemas.microsoft.com/BizTalkServer/2004/10/BAM">
            XmlNamespaceManager _mgr = new XmlNamespaceManager(BAMExportedDocument.NameTable);
            _mgr.AddNamespace("ns0", "http://schemas.microsoft.com/BizTalkServer/2004/10/BAM");

            //Get the root document
            string xPath = "/ns0:BAMDefinition/ns0:Activity";

            XmlNodeList childNodes = BAMExportedDocument.SelectNodes(xPath, _mgr);
            XmlDocument _returnDoc = new XmlDocument();
            //This stringbuilder will hold the XML output for loading into a document.  An alternative approach would be to use
            //strongly typed XML Elements and Attributes.
            StringBuilder _returnXML = new StringBuilder();

            _returnXML.AppendLine(@"<xs:schema xmlns:b=""http://schemas.microsoft.com/BizTalk/2003"" targetNamespace=""" + NameSpace + @""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">");

            _returnXML.AppendLine(@"<xs:element name=""BAM_Activity_Schema""><xs:complexType><xs:choice>");


            foreach (XmlNode node in childNodes)
            {
                Console.WriteLine("Writing Child Node:" + node.Attributes["Name"].Value);
                //_returnXML.AppendLine(@"<xs:element name=""CLM_837I"">");
                _returnXML.AppendLine(@"<xs:element maxOccurs=""unbounded"" name=""" + node.Attributes["Name"].Value + @""">");
                _returnXML.AppendLine(@"<xs:complexType><xs:sequence>");

                foreach (XmlNode Activities in node.ChildNodes)
                {
                    //Map the data types coming from the BAM Export to XSD types
                    _returnXML.AppendLine(@"<xs:element name=""" + Activities.Attributes["Name"].Value + @""" minOccurs=""0""");

                    string nodeName = Activities.Attributes["Name"].Value;
                    if (Activities.Attributes["DataType"].Value == "NVARCHAR")
                    {
                        _returnXML.AppendLine(@" type=""xs:string""/>");
                    }
                    if (Activities.Attributes["DataType"].Value == "DATETIME")
                    {
                        _returnXML.AppendLine(@" type=""xs:dateTime""/>");
                    }
                    if (Activities.Attributes["DataType"].Value == "FLOAT")
                    {
                        _returnXML.AppendLine(@" type=""xs:float""/>");
                    }
                    if (Activities.Attributes["DataType"].Value == "INT")
                    {
                        _returnXML.AppendLine(@" type=""xs:int""/>");
                    }
                }
                _returnXML.AppendLine(@"</xs:sequence></xs:complexType>");
                _returnXML.AppendLine(@"</xs:element>");
            }




            _returnXML.AppendLine(@"</xs:choice></xs:complexType></xs:element>");
            _returnXML.AppendLine(@"</xs:schema>");
            return _returnXML.ToString();

        }
        public static string ChildXML(XmlDocument BAMExportedDocument, string ActivityID)
        {
            //Used to build a new XSD document for BAM tracking
            //Parse the existing document 
            //Root - <BAMDefinition xmlns="http://schemas.microsoft.com/BizTalkServer/2004/10/BAM">
            XmlNamespaceManager _mgr = new XmlNamespaceManager(BAMExportedDocument.NameTable);
            _mgr.AddNamespace("ns0", "http://schemas.microsoft.com/BizTalkServer/2004/10/BAM");

            //Get the root document
            string xPath = "/ns0:BAMDefinition/ns0:Activity";

            XmlNodeList childNodes = BAMExportedDocument.SelectNodes(xPath, _mgr);
            StringBuilder _returnXML = new StringBuilder();

           
            #region Root XML
            foreach (XmlNode node in childNodes)
            {
                
                if (node.Attributes["Name"].Value == ActivityID)
                {
                    Console.WriteLine("Writing Child Node:" + ActivityID);
                   
                    #region Complex Type XML
                    foreach (XmlNode Activities in node.ChildNodes)
                    {
                        //Map the data types coming from the BAM Export to XSD types
                        _returnXML.AppendLine(@"<xs:element name=""" + Activities.Attributes["Name"].Value + @""" minOccurs=""0""");

                        string nodeName = Activities.Attributes["Name"].Value;
                        string nodeType = "";
                        if (Activities.Attributes["DataType"].Value == "NVARCHAR")
                        {
                            _returnXML.AppendLine(@" type=""xs:string""/>");
                        }
                        if (Activities.Attributes["DataType"].Value == "DATETIME")
                        {
                            _returnXML.AppendLine(@" type=""xs:dateTime""/>");
                        }
                        if (Activities.Attributes["DataType"].Value == "FLOAT")
                        {
                            _returnXML.AppendLine(@" type=""xs:float""/>");
                        }
                        if (Activities.Attributes["DataType"].Value == "INT")
                        {
                            _returnXML.AppendLine(@" type=""xs:int""/>");
                        }
                    }
                    Console.WriteLine("\tChild Node Count:" + node.ChildNodes.Count);
                    #endregion
                   
                    
                }
            }
            return _returnXML.ToString();
            #endregion
        }
    }